package com.kyzh.core.pager.me.user

import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.gushenge.core.base.activity.BaseVmDbActivity
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.databinding.ActScanloginBinding
import kotlinx.coroutines.*
import org.jetbrains.anko.sdk27.coroutines.onClick


class ScanLoginActivity :BaseVmDbActivity<UserViewModel,ActScanloginBinding>(R.layout.act_scanlogin) {
    companion object{
        @JvmStatic
        fun start(context: Context,token:String) {
            val starter = Intent(context, ScanLoginActivity::class.java)
            starter.putExtra("token",token)
            context.startActivity(starter)
        }
    }
    var token :String?  = ""
    private var progressDialog : ProgressDialog?= null
    override fun initView(savedInstanceState: Bundle?) {
        token = intent.getStringExtra("token") .toString()
        progressDialog = ProgressDialog(this)
        progressDialog?.setCancelable(false)
        progressDialog?.setMessage("登录中，请稍后..")

        mDatabind.apply {
            login.onClick {
                progressDialog?.show()
                mViewModel.sacn(token){
                    GlobalScope.launch {
                        withContext(Dispatchers.Main){
                            progressDialog?.dismiss()
                            "登录成功".toast()
                        }
                        delay(2000)
                        withContext(Dispatchers.Main){
                            finish()
                        }
 
                    }
                }
            }
            close.onClick {
                finish()
            }
            cancel.onClick {
                finish()
            }
        }

    }

}